//
//  AELWeatherRecordController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/29.
//

import UIKit

class AELWeatherRecordController: AELBaseViewController {
    @IBOutlet var tableView: UITableView!
    
    @IBOutlet var weatherIcon: UIImageView!
    @IBOutlet var locationLabel: UILabel!
    @IBOutlet var weekLabel: UILabel!
    @IBOutlet var dateLabel: UILabel!
    @IBOutlet var temperatureLabel: UILabel!
    lazy var viewModel: AELWeatherViewModel = {
        return AELWeatherViewModel()
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
        getPageData()

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTab()
        // Do any additional setup after loading the view.
    }
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "AELWeatherRecordCell", bundle: .main), forCellReuseIdentifier: "AELWeatherRecordCell")
    }
    func getPageData(){
        self.viewModel.loadData { [self] models in
            setTopViewData()
            self.tableView.reloadData()
        }
    }
    func setTopViewData(){
        let model = self.viewModel.dataSource.first
        self.weatherIcon.image = UIImage(named: WEATHER_ICONS[model!.weather])
        self.locationLabel.text = model?.location
        self.dateLabel.text = model?.date
        self.weekLabel.text = model?.week
        self.temperatureLabel.text = model?.temperature
    }
    @IBAction func ckickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickAddWeatherAction(_ sender: Any) {
        self.navigationController?.pushViewController(AELAddWeatherController(), animated: true)
    }
    
}
extension AELWeatherRecordController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:AELWeatherRecordCell = tableView.dequeueReusableCell(withIdentifier: "AELWeatherRecordCell") as! AELWeatherRecordCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
